package com.da.mybatis.service;

import java.util.List;

import javax.annotation.Resource;

import com.da.mybatis.bean.Student;
import com.da.mybatis.bean.StudentDTO;
import com.da.mybatis.convertor.StudentConvertor;
import com.da.mybatis.mapper.StudentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author JokerDa.
 * @date 2020/12/25 10:16
 * Description:
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private StudentConvertor convertor;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addStudentTest() {
        Student student = new Student();
        student.setAge(99);
        student.setName("test");
        studentMapper.insert(student);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addStudentTestError() {
        Student student = new Student();
        student.setAge(88);
        student.setName("test1");
        studentMapper.insert(student);
        throw new RuntimeException("exception");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStudentTest() {
        Student student = new Student();
        student.setAge(99);
        student.setName("test");
        student.setId(7);
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStudentTestError() {
        Student student = new Student();
        student.setAge(88);
        student.setName("test1");
        student.setId(7);
        studentMapper.updateByPrimaryKey(student);
        throw new RuntimeException("exception");
    }

    @Override
    public List<StudentDTO> queryAllStudent() {
        List<Student> students = studentMapper.queryAll();
        return convertor.convertList(students);
    }
}
